#include "RadarWidgit.h"
#include <QPainter>
#include <QTimerEvent>
#include <QConicalGradient>
#include <qDebug>
#include <QOpenGLWindow>
#include <QOpenGLWidget>

RadarWidgit::RadarWidgit(QWidget *parent) : QWidget(parent)
{
    //初始化
    m_backColor = QColor(80,80,80);     //初始化背景颜色
    m_drawColor = QColor(255,255,255);  //初始化绘制颜色
    m_fanColor  = QColor(0,180,0);      //初始化扫描扇叶颜色
    m_pieRotate = 0;
    m_nSpeed    = 50;
    m_points<<QPoint()<<QPoint()<<QPoint()<<QPoint()<<QPoint();

    //启动定时器
    connect(&fanTime,&QTimer::timeout,this,[=]()    //驱动扫描
    {
        static int lastLen = 150;
        m_pieRotate -= 5;   //每次扫描角度变动
        if(m_pieRotate <= -360) m_pieRotate = 0;
        int len = rand()%80 + lastLen;

        angleMapLen[m_pieRotate] = len;
        //angleMapLen.insert(m_pieRotate,len);
        update();
    });
    fanTime.start(m_nSpeed);    //扫描间隔，扫描速度等于扫描间隔+扫描角度
}

void RadarWidgit::setBackColor(QColor color)
{
    m_backColor = color;
}
void RadarWidgit::setDrawColor(QColor color)
{
    m_drawColor = color;
}
void RadarWidgit::setFanColor(QColor color)
{
    m_fanColor = color;
}
void RadarWidgit::setFanSpeed(int speed)
{
    m_nSpeed = speed;
    fanTime.setInterval(m_nSpeed);
}

void RadarWidgit::setAim(int angle, double len)
{
    angleMapLen.insert(angle,len);  //更新或者添加
}

void RadarWidgit::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);    //抗锯齿
    painter.fillRect(rect(),m_backColor);    //背景
    int len = m_drawArea.width();    //边长

    //============== 绘制需要扫描区域 ==============
    painter.setPen(Qt::NoPen);
    QBrush brush(QColor(0, 0, 255, 50), Qt::Dense1Pattern);
    painter.setBrush(brush);    //使用画刷
    painter.drawEllipse(m_drawArea.center(),len/2,len/2);

    //============== 绘制无障碍区域 ==================
    painter.setPen(QPen(QColor(255,255,255),2));

    QPointF points[angleMapLen.size() + 4] = {m_drawArea.center()};
    QMap<int,double>::iterator item;
    int i = 0;
    for ( item = angleMapLen.begin(); item != angleMapLen.end(); item ++ )
    {
        int angle = item.key();
        int len = item.value();
        if(len > 1000)  continue;
        QPoint dotPos = angleLenToXY(angle,len) + m_drawArea.center();
        points[i ++] = dotPos;
        painter.drawPoint(dotPos);
    }
    painter.setPen(Qt::NoPen);
    QBrush brush2(m_backColor, Qt::Dense1Pattern);     //创建画刷
    painter.setBrush(brush2);    //使用画刷
    painter.drawPolygon(points, angleMapLen.size());    //绘制多边形,绘制为背景颜色

    //============== 绘制背景z坐标（圆+十字）==============
    painter.setPen(QPen(QColor(255, 255, 255,100),2));
    painter.setBrush(Qt::NoBrush);    //使用画刷
    painter.drawLine(m_drawArea.topLeft() + QPoint(0,len/2),m_drawArea.topRight() + QPoint(0,len/2));
    painter.drawLine(m_drawArea.topLeft() + QPoint(len/2,0),m_drawArea.bottomLeft() + QPoint(len/2,0));
    painter.drawEllipse(m_drawArea.center(),len/2,len/2);
    painter.drawEllipse(m_drawArea.center(),len/3,len/3);
    painter.drawEllipse(m_drawArea.center(),len/6,len/6);

    //======================== 绘制转动扇叶 =============================
    //---线
    qreal x = m_drawArea.center().x() + (qreal)len/2 * cos(-m_pieRotate*3.14159/180);
    qreal y = m_drawArea.center().y() + (qreal)len/2 * sin(-m_pieRotate*3.14159/180);
    painter.setPen(QPen(m_drawColor));
    painter.drawLine(m_drawArea.center(),QPointF(x,y));
    //---扇形
    QConicalGradient gradient;
    gradient.setCenter(m_drawArea.center());
    gradient.setAngle(m_pieRotate + 180); //渐变与旋转方向恰好相反，以扇形相反的边作为渐变角度。

    gradient.setColorAt(0.4,QColor(m_fanColor.red(),m_fanColor.green(),m_fanColor.blue(),255)); //从渐变角度开始0.5 - 0.75为扇形区域，由于Int类型计算不精确，将范围扩大到0.4-0.8
    gradient.setColorAt(0.8,QColor(m_fanColor.red(),m_fanColor.green(),m_fanColor.blue(),0));
    painter.setBrush(QBrush(gradient));
    painter.setPen(Qt::NoPen);
    painter.drawPie(m_drawArea,m_pieRotate*16,90*16);
}

void RadarWidgit::resizeEvent(QResizeEvent *event)
{
    //以较短的边长作为绘制区域边长
    if(width() > height())
        m_drawArea = QRect((width() - height())/2,0,height(),height());
    else
        m_drawArea = QRect(0,(height() - width())/2,width(),width());

    m_drawArea.adjust(10,10,-10,-10);

    m_points.clear();
    QMap<int,double>::iterator item;
    for ( item = angleMapLen.begin(); item != angleMapLen.end(); item ++ )
    {
        int angle = item.key();
        int len = item.value();
        if(len > 1000)  continue;
        QPoint dotPos = angleLenToXY(angle,len) + m_drawArea.center();
        m_points.append(dotPos);
    }
}

QPoint RadarWidgit::angleLenToXY(int angle,double len)
{
    int a = sin(30);
    int x = len*cos(angle*2*M_PI/360);
    int y = -len*sin(angle*2*M_PI/360);
    return QPoint(x,y);
}


